package com.example.demo.service.impl;

import com.example.demo.client.evt.sysbasequery.LogStatisticEvt;
import com.example.demo.client.vm.sysbasequery.HistogramVm;
import com.example.demo.client.vm.sysbasequery.LogStatisticsDataVm;
import com.example.demo.client.vm.sysbasequery.LogStatisticsVm;
import com.example.demo.enums.LogStatisticsTitle;
import com.example.demo.service.BaseService;
import com.example.demo.service.LogStatisticService;
import com.example.demo.util.excel.ExcelCreateUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author zhengmf
 * @Date 2019/1/15  14:36
 */
@Service
public class LogStatisticServiceImpl extends BaseService implements LogStatisticService {
    @Override
    public Workbook allDataWorkbook() {
        XSSFWorkbook workbook = new XSSFWorkbook();
        LogStatisticsDataVm vm = baseQueryClient.getLogStatisticsData().getBody();
        for (HistogramVm histogramVm: vm.getDataList()){
            addSheetData(workbook,histogramVm);
        }
        return workbook;
    }

    @Override
    public Workbook selectedDataWorkbook(List<String> selectedTitle) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        LogStatisticsDataVm vm = baseQueryClient.getLogStatisticsData().getBody();
        for (String selected:selectedTitle) {
            for (HistogramVm histogramVm: vm.getDataList()){
                if(selected.equals(histogramVm.getKey())){
                    addSheetData(workbook,histogramVm);
                }
            }
        }
        return workbook;
    }

    private void addSheetData(Workbook workbook, HistogramVm histogramVm){
        List<String> keys = Arrays.asList("name","count");
        List<Map<String, Object>> data = ExcelCreateUtil.parseListMap(histogramVm.getData(), keys);
        ExcelCreateUtil.addSheet(workbook,data,keys,Arrays
            .asList(histogramVm.getxName(),histogramVm.getyName()), histogramVm.getTitle());
        addDrillData(histogramVm,keys,workbook);
    }

    /**
     * 添加下钻数据
     * @param histogramVm
     * @param keys
     * @param workbook
     */
    private void addDrillData(HistogramVm histogramVm, List<String> keys, Workbook workbook){
        LogStatisticEvt logStatisticEvt = new LogStatisticEvt();
        //系统功能TOP10使用下钻数据
        if(histogramVm.getKey().equals(LogStatisticsTitle.FUNCTION_USE_TOP10.name())){
            for (LogStatisticsVm logStatisticsVm:histogramVm.getData()){
                logStatisticEvt.setName(logStatisticsVm.getName());
                HistogramVm drill = baseQueryClient.getFunctionUseTop10Drill(logStatisticEvt).getBody();
                List<Map<String, Object>> functionUseTop10Drill = ExcelCreateUtil.parseListMap(drill.getData(), keys);
                ExcelCreateUtil.addSheet(workbook,functionUseTop10Drill,keys,Arrays
                    .asList(histogramVm.getxName(),histogramVm.getyName()), drill.getTitle());
            }
        }
        //用户个人功能使用TOP10下钻数据
        if(histogramVm.getKey().equals(LogStatisticsTitle.USER_FUNCTION_USE_TOP10.name())){
            for (LogStatisticsVm logStatisticsVm:histogramVm.getData()){
                logStatisticEvt.setName(logStatisticsVm.getName());
                HistogramVm drill = baseQueryClient.getUserFunctionUseTop10Drill(logStatisticEvt).getBody();
                List<Map<String, Object>> userFunctionUseTop10Drill = ExcelCreateUtil.parseListMap(drill.getData(), keys);
                ExcelCreateUtil.addSheet(workbook,userFunctionUseTop10Drill,keys,Arrays
                    .asList(histogramVm.getxName(),histogramVm.getyName()), drill.getTitle());
            }
        }
    }

}
